package com.example.agent_intelligent_companion;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	// This variables are for the dropdown listview.
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
//    SQLiteOpenHelper dbHelper;
//    SQLiteDatabase database;
    
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
//        dbHelper = new AICDatabase(this);
//        database = dbHelper.getWritableDatabase();
        
        /* Scan button event listnener */
        Button scanBtn = (Button)findViewById(R.id.scanButton);
        
        scanBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//Toast.makeText(getApplicationContext(), "Custom Button Pressed", Toast.LENGTH_SHORT).show();
				ScanBluetooth();
			}
		});
 
        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
 
        // preparing list data
        prepareListData();
 
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
 
        // setting list adapter
        expListView.setAdapter(listAdapter);
 
        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

        	@Override
        	public boolean onChildClick(ExpandableListView parent, View v,
        			int groupPosition, int childPosition, long id) {
        		// TODO Auto-generated method stub
        		Toast.makeText(
        				getApplicationContext(),
        				listDataHeader.get(groupPosition)
        				+ " : "
        				+ listDataChild.get(
        						listDataHeader.get(groupPosition)).get(
        								childPosition), Toast.LENGTH_SHORT)
        								.show();
        		if(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).toString() == "Settings") // could use group position but this is safer.
        		{
        			loadSettings();
        		}
        		else if(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).toString() == "Update DataBase")
        		{
        			loadDatabase();
        		}
        		return false;
        	}
        });
    }
    
    private void loadDatabase()
    {
    	Intent intent = new Intent(this, DatabaseActivity.class);
    	startActivity(intent);
    }

	private void loadSettings()
    {
    	Intent intent = new Intent(this, UserSettingsActivity.class);
    	startActivity(intent);
    }
    
    private void ScanBluetooth()
    {
    	Intent intent = new Intent(this, BlueToothActivity.class);
    	startActivity(intent);
    }
    
    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
 
        // Adding child data
        listDataHeader.add("Menu");
 
        // Adding child data
        List<String> settings = new ArrayList<String>();
        settings.add("Settings");
        settings.add("Update DataBase");
        settings.add("Log Out");

        listDataChild.put(listDataHeader.get(0), settings); // Header, Child data
    }
}
